/**
 * Created by Jack Xue on 20101202
 */
package org.code.jack.jweberp.web;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.code.jack.jweberp.domain.SystemDictionary;
import org.code.jack.jweberp.domain.SystemDictionaryDetail;
import org.code.jack.jweberp.service.ISystemDictionaryDetailService;
import org.code.jack.jweberp.service.ISystemDictionaryService;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;

@Controller("/systemDictionaryDetail")
public class SystemDictionaryDetailAction extends BaseAction {
	@Resource
	private ISystemDictionaryService systemDictionaryService;

	@Resource
	private ISystemDictionaryDetailService systemDictionaryDetailService;

	public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
//		SystemDictionaryDetailQuery qo = (SystemDictionaryDetailQuery) webForm.form2Obj(SystemDictionaryDetailQuery.class);
//		IPageList pageList = systemDictionaryDetailService.getByQueryObject(qo);
//		CommUtil.IPageListToWebView(pageList, request);
		return mapping.findForward("list");
	}

	public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
		String parentId = (String) request.getParameter("parentId");
		if (StringUtils.hasLength(parentId)) {
			SystemDictionary sd = systemDictionaryService.getEntity(new Long(parentId));
			request.setAttribute("parent", sd);
		}
		return mapping.findForward("edit");
	}

	public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		if (StringUtils.hasText(id)) {
			SystemDictionaryDetail obj = systemDictionaryDetailService.getEntity(new Long(id));
			request.setAttribute("obj", obj);
		}
		return mapping.findForward("edit");
	}

	public ActionForward update(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		if (StringUtils.hasText(id)) {
			SystemDictionaryDetail dep = systemDictionaryDetailService.getEntity(new Long(id));
			if (dep != null) {
				webForm.form2Obj(dep, true);
				systemDictionaryDetailService.updateEntity(dep);
				request.setAttribute("msg", "更新成功");
			}
		}
		request.setAttribute("newurl", "systemDictionaryDetail.do?cmd=list&parentId=" + request.getParameter("parentId"));
		return this.list(mapping, form, request, response);
	}

	public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
		SystemDictionaryDetail sdd = (SystemDictionaryDetail) webForm.form2Obj(SystemDictionaryDetail.class, true);
		systemDictionaryDetailService.createEntity(sdd);
		request.setAttribute("msg", sdd.getId() != null ? "添加成功" : "添加失败");
		request.setAttribute("newurl", "systemDictionaryDetail.do?cmd=list&parentId=" + request.getParameter("parentId"));
		return list(mapping, form, request, response);
	}

	public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		if (StringUtils.hasLength(id)) {
			Boolean b = systemDictionaryDetailService.deleteEntity(new Long(id));
			request.setAttribute("msg", b ? "删除成功" : "删除失败");
			request.setAttribute("newurl", "systemDictionaryDetail.do?cmd=list&parentId=" + request.getParameter("parentId"));
		}
		return list(mapping, form, request, response);
	}

	@Override
	public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
		return index(mapping, form, request, response);
	}

	public ActionForward index(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
		return list(mapping, form, request, response);
	}
}
